import {delay, toast} from "../../utils/tool";
import EventBus from "../../utils/eventBus";
import {BASE_URL, SOURCE_URL} from "../../constant/index";

const app = getApp()
Page({
    data: {
        storeList: [],
        type: '',
        loading: false,
        baseUrl: SOURCE_URL,
    },
    changeStoreHandle(e) {
        // if (this.data.loading) return
        // this.data.loading = true
        // const {store} = e.currentTarget.dataset
        // app.setFirst(store)
        // const {type} = this.data
        // toast({title: '选择成功'}, () => {
        //     this.data.loading = false
        //     EventBus.$emit('change_store_success', store)
        //     if (type === 'back') {
        //         return wx.navigateBack()
        //     }
        //     app.globalData.shouldUpdate = false
        //     wx.switchTab({
        //         url: "/pages/index/index",
        //     })
        // })

        if (this.data.loading) return
        this.data.loading = true
        const {store} = e.currentTarget.dataset
        app.setFirst(store)
        const {type} = this.data
        app.globalData.changeStore = store
        toast({title: '选择成功'}, () => {
            this.data.loading = false
            if (type === 'back') return  wx.navigateBack()
            wx.switchTab({
                url: "/pages/index/index",
            })
        })

    },
    // 打开地图
    openMap: function (e) {
        let mapdata = e.currentTarget.dataset.mapdata
        let arr = mapdata.lat.split(",")
        var lat = parseFloat(arr[1]),
            lon = parseFloat(arr[0]),
            firstStore = this.data.firstStore;
        if (!lat || !lon) {
            return false;
        }
        wx.openLocation({
            latitude: lon,
            longitude: lat,
            scale: 15,
            name: mapdata.name,
            address: mapdata.address,
            success: function (res) {
            },
            fail: function (res) {
            }
        })
    },
    previewImage(ev) {
        const {image} = ev.currentTarget.dataset
        if (!image) return
        wx.previewImage({
            urls: [image]
        })
    },

    getStore(e) {
        wx.showLoading({title: '正在获取', mask: true})
        app.ajax({
            url: "/api/room/storelist",
            data: {
                lat: e.lat,
                lon: e.lon
            },
            success: async res => {
                await delay(200)
                if (!res.data || !res.data.length) {
                    res.data = []
                }
                const {code, data, msg} = res
                wx.hideLoading()
                if (code !== 1) return toast({title: msg})
                this.setData({
                    storeList: data.map(item => {
                        item.image = SOURCE_URL + item.image
                        return item
                    })
                })
            }
        })
    },
    callPhone(e) {
        wx.makePhoneCall({
            phoneNumber: e.currentTarget.dataset.phone,
            success: function () {
            },
        })
    },
    onShow() {
        if (this.data.storeList.length) return
        let location = wx.getStorageSync('location')
        this.getStore(location)
    },
    onLoad(options) {
        if (options.hasOwnProperty('type')) {
            const {type} = options
            this.data.type = type
        }
    },

})
